<?php

class Forms_GiteForm extends Zend_Dojo_Form {

	public function setName($name)
	{
		parent::setName($name);
		$this->getView()->js()->dojo();
		$this->getView()->validateDojoForm($this->getName());
		return $this;
	}

	public function addElement($element, $name = null, $options = null)
	{
		if (!$this->translatorIsDisabled()) {
			if (is_array($options) && array_key_exists('invalidMessage', $options))
			{
				$invalidMessage = $options['invalidMessage'];
				if (is_string($invalidMessage)) {
					$options['invalidMessage'] = $this->getTranslator()->translate($invalidMessage);
				}
				else if (is_array($invalidMessage) && count($invalidMessage) > 1) {
					$message = $this->getTranslator()->translate(array_shift($invalidMessage));
					foreach ($invalidMessage as $key => $value) {
						$message = str_replace("'%".$key."%'", $value, $message);
					}
					$options['invalidMessage'] = $message;
				}
				else {
					$options['invalidMessage'] = 'invalidMessage';
				}				
			}
		}
		parent::addElement($element, $name, $options);
	}
	
	public function makeFirstname($name="prenom", $label="form.label.firstname") {
		$this->addElement('ValidationTextBox', $name, array(
        	'label'				=> $label,
			'trim'				=> true,
        	'required'       	=> true,
        	'invalidMessage'	=> 'form.validation.error.firstname',
			'maxlength'         => 40,
			'style'      		=> 'width: 10em;',
		));
	}
	
	public function makeName($name="nom", $label="form.label.lastname") {
		$this->addElement('ValidationTextBox', $name, array(
        	'label'				=> $label,
			'trim'				=> true,
        	'required'       	=> true,
        	'invalidMessage'	=> 'form.validation.error.lastname',
			'maxlength'         => 40,
			'style'      		=> 'width: 10em;',
		));
	}

	public function makeEmail($name="email", $label="form.label.email") {
		$this->addElement('ValidationTextBox', $name, array(
        	'label'    			=> $label,
			'filters'			=> array('StringToLower'),
			'trim' 				=> true,
        	'required' 			=> true,
			'regExpGen' 		=> "dojox.validate.regexp.emailAddress",
        	'validators' 		=> array('EmailAddress'), 
        	'invalidMessage' 	=> 'form.validation.error.email',
		));
	}
	
	public function makeObject($name="objet", $label="form.label.object") {
		$this->addElement('ValidationTextBox', $name, array(
        	'label'				=> $label,
			'trim'				=> true,
        	'required'       	=> true,
        	'invalidMessage'	=> 'form.validation.error.object',
			'maxlength'         => 100,
			'style'      		=> 'width: 440px',
		));
	}
	
	public function makeEditorMessage($name="message", $label="form.label.message") {
		$this->addElement('editor', $name, array(
			'label'				=> $label,
			'cols'				=> 5,
			'trim'				=> true,
        	'required'       	=> true,
			'validators' 		=> array('notEmpty'),
        	'invalidMessage'	=> 'form.validation.error.string',
			'inheritWidth'       => false,
			'style'      		=> 'width: 440px',
			'width'      		=> 440,
			'height'			=> 150
		));
	}
	
	public function makeSimpleMessage($name="message", $label="form.label.message") {
		$this->addElement('SimpleTextarea', $name, array(
        	'label'    => $label,
			'trim'	   => true,
        	'required' => true,
        	'invalidMessage' => 'Invalid non-space text.',
        	'style'    => 'width: 440px; height: 150px;',
    	));
	}

	public function makeCaptcha($name="captcha", $label="form.label.captcha") {
		$attributsCaptcha = array(
			Utils_Captcha::PARAM_ID => Utils_Captcha::getNewId(),
			Utils_Captcha::PARAM_TYPE => 'gif',
			Utils_Captcha::PARAM_LENGTH => 4,
			Utils_Captcha::PARAM_SIZE => 15);
		$captcha = new Forms_Elements_Captcha($name.'Image', $attributsCaptcha);
		$captcha->setLabel('form.label.captcha.image');
		
		$this->addElement('ValidationTextBox', $name.'Input', array(
			'label'				=> 'form.label.captcha.input',
			'value' 			=> '',
			'required'			=> true,
			'trim' 				=> "true",
			'regExp' 			=> '[A-Za-z0-9]{'.$attributsCaptcha['stringLength'].'}',
			'invalidMessage'	=> array('message' => "form.validation.error.captcha '%nbCaractere%'", 'nbCaractere' => $attributsCaptcha[Utils_Captcha::PARAM_LENGTH]),
			'validators' 		=> array(new Validate_Captcha()),
			'style' 			=> "width: ".$attributsCaptcha['stringLength']."em;",
			'maxlength' 		=> $attributsCaptcha['stringLength'],
		));

		$this->addElement($captcha);
		
		$this->groupInline(array($name.'Input', $name.'Image'), 'captchaGroup', array('legend' => ''));
		
		$this->getView()->js()
		->jQueryTipsy()
		->addReadyScript("$('#tablecaptchaGroup').tipsy({gravity: 'w',fallback: '".$this->getView()->translate('form.label.captcha.explication')."'});");
	}

	public function makeSubmit($name="envoyer", $label="form.label.submit") {
		$this->addElement('SubmitButton', $name, array(
			'required'   		=> false,
        	'ignore'     		=> true,
        	'label'      		=> $label,
		));
	}
	
	public function groupInline(array $elements, $name, $attribs) {
		$i = 0;
		foreach ($elements as $element) {
			$this->getElement($element)->addDecorator(new Forms_Decorators_Wrap(array('tag' => 'td', 'class' => 'td'.$i)));
			$i ++;
		}
		$this->addDisplayGroup($elements, $name, $attribs);
		
		$group = $this->getDisplayGroup($name);		
		$group->clearDecorators();
        $group->setDecorators(array(
                    'FormElements',
                    array('HtmlTag', array('tag'=>'tr')),
                    'FormElements',
                    new Forms_Decorators_Wrap(array('tag' => 'table', 'id' => 'table'.$name))
        ));
	}	
}